package com.version1.diabeco.ui.comida;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.version1.diabeco.R;
import com.version1.diabeco.data.comida.AlimentoDaoImpl;
import com.version1.diabeco.model.alimento.Alimento;
import com.version1.diabeco.model.comida.DetalleComida;

public class ActivityRegistroDetalleComida extends Activity {

	private static final int REQUEST_CODE = 1234;

	private List<Alimento> alimentos;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_registro_detalle_comida);

		this.cargarPantalla();

	}

	private void cargarPantalla() {

		this.setAlimentos(AlimentoDaoImpl.getInstance(this).selectAll());

		List<Alimento> alimentos = this.getAlimentos();

		if (!alimentos.isEmpty()) {
			ListView measuresList = (ListView) findViewById(R.id.list_registros_alimento);

			ArrayAdapter<Alimento> measuresAdapter = new ArrayAdapter<Alimento>(this,
					android.R.layout.simple_list_item_1, alimentos);
			measuresList.setAdapter(measuresAdapter);

			measuresList.setOnItemClickListener(new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

					ArrayAdapter<Alimento> arrayAdapter = (ArrayAdapter<Alimento>) ((ListView) parent).getAdapter();
					Alimento alimento = arrayAdapter.getItem(position);

					Intent intent = ((Activity) parent.getContext()).getIntent();
					DetalleComida detalleComida = (DetalleComida) intent
							.getSerializableExtra(ActivityRegistroComida.INTENT_VALUE_DETALLE_COMIDA);
					detalleComida.setAlimento(alimento);
					detalleComida.setCarbohidratosAjustados(alimento.getCarbohidratos());

					intent.putExtra(ActivityRegistroComida.INTENT_VALUE_DETALLE_COMIDA, detalleComida);

					setResult(Activity.RESULT_OK, intent);
					finish();
				}

			});

		} else {
			// TODO inform the user there are no comidas taken
		}

	}

	public List<Alimento> getAlimentos() {
		return alimentos;
	}

	public void setAlimentos(List<Alimento> alimentos) {
		this.alimentos = alimentos;
	}

}
